package com.scoy.honeymei.activity.login;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scoy.honeymei.R;
import com.scoy.honeymei.activity.MainActivity;
import com.scoy.honeymei.activity.film.FilmCinemaListActivity;
import com.scoy.honeymei.activity.home.FilmDetailActivity;
import com.scoy.honeymei.activity.home.TravelDetailActivity;
import com.scoy.honeymei.activity.home.TripDetailActivity;
import com.scoy.honeymei.activity.mall.GoodsDetailActivity;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.AdsBean;
import com.scoy.honeymei.bean.FilmBean;
import com.scoy.honeymei.custem.AdsVideoPlayer;
import com.scoy.honeymei.custem.MeVideoView;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.GlideImgUtil;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.Jzvd;
import cn.jzvd.JzvdStd;

/**
 * ClassName:      com.scoy.honeymei.activity.login
 * Description:
 * Author:         GaoZhan & Administrator
 * UpdateDate:     2019/12/7/0007$ 上午 9:21$
 * UpdateRemark:   更新内容
 */
public class Ads2Activity extends BaseActivity {

    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.time_tv)
    TextView timeTv;
    @BindView(R.id.item_cl)
    ConstraintLayout itemCl;
    @BindView(R.id.img_iv)
    ImageView imgIv;
    @BindView(R.id.video_view)
    AdsVideoPlayer videoView;
    private Ads2Activity mContext;
    private CountDownTimer countTimer;

    private int jpType = 0;
    private int tzId;
    private String urlLink;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ads);
        ButterKnife.bind(this);
        mContext = Ads2Activity.this;

        initNormal();

        httpImg();

    }

    @Override
    public void initNormal() {

        MyUtil.setStatusBar(mContext, getWindow(), true, 0);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        countTimer = new CountDownTimer(1000 * 5, 500) {
            @Override
            public void onTick(long millisUntilFinished) {
                int leftTime = (int) millisUntilFinished / 1000;
                timeTv.setEnabled(false);
                timeTv.setText(String.format(getResources().getString(R.string.finishnow0), leftTime));
            }

            @Override
            public void onFinish() {
                timeTv.setEnabled(true);
                timeTv.setText(" 跳过 ");
            }
        }.start();

    }

    @OnClick({R.id.time_tv, R.id.item_cl})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.time_tv:
                onClicked();
                break;
            case R.id.item_cl:
                if ("null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && "null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
                    startActivity(new Intent(mContext, LoginActivity.class));
                    finish();
                    return;
                }
                jumpTo(jpType);
                break;
            default:
                break;

        }
    }

    private void initData(String urlStr) {
        Glide.with(this)
                .load(urlStr)
                .into(videoView.thumbImageView);

        videoView.setUp(urlStr, null);
        videoView.startVideo();

        videoView.setOnUIClick(new AdsVideoPlayer.OnUIClick() {
            @Override
            public void uiClick() {
                if ("null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && "null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
                    startActivity(new Intent(mContext, LoginActivity.class));
                    finish();
                    return;
                }
                jumpTo(jpType);
            }

            @Override
            public void playFinish() {
                onClicked();
            }
        });

    }

    private void onClicked() {
        if (countTimer != null) {
            countTimer.cancel();
        }
        if (!"null".equals(SPHelper.getString(MeConstant.PHONE, "null")) && !"null".equals(SPHelper.getString(MeConstant.UID, "null"))) {
            startActivity(new Intent(mContext, MainActivity.class));
        } else {
            startActivity(new Intent(mContext, LoginActivity.class));
        }
        finish();
    }


    private void httpImg() {
        OkGo.<String>post(MyUrl.APP_AD_IMG).execute(new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                String body = response.body();
                try {
                    JSONObject jsBean = new JSONObject(body);
                    String code = jsBean.getString("code");
                    String msg = jsBean.getString("msg");
                    Object data = jsBean.get("data");
                    String sdata = data.toString();

                    if ("200".equals(code)) {
                        JSONObject jsb = new JSONObject(sdata);
                        if(jsb.getInt("gg_type")==2){
                            //广告联盟
                            return;
                        }
                        String app = jsb.getString("app");
                        if ("".equals(app) || "null".equals(app)) {
                            onClicked();
                            return;
                        }
                        AdsBean adsBean = new Gson().fromJson(app, AdsBean.class);
                        //跳转类型，0=不跳转、1=外部链接、2=商品、3=旅游、4=出行、5=电影
                        jpType = adsBean.getTz_type();
                        tzId = adsBean.getTz_id();
                        urlLink = adsBean.getUrl();

                        //类型，1=图片、2=视频
                        int imgVideo = adsBean.getImg_video();
                        if (imgVideo == 1) {
                            String image = adsBean.getImage();
                            if (image == null || "".equals(image)) {
                                onClicked();
                            } else {
                                GlideImgUtil.glidePic(mContext, image, imgIv);
                            }

                        } else if (imgVideo == 2) {
                            String video = adsBean.getVideo();
                            if (video == null || "".equals(video)) {
                                onClicked();
                            } else {
                                imgIv.setVisibility(View.GONE);
                                initData(MyUrl.PREFIX_PIC + video);
                            }
                        } else {
                            onClicked();
                        }


                    } else {
                        onClicked();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                onClicked();
            }
        });


    }

    private void jumpTo(int type) {
        //跳转类型，0=不跳转、1=外部链接、2=商品、3=旅游、4=出行、5=电影
        switch (type) {

            case 0:

                break;
            case 1:
                Intent intentMain1 = new Intent(mContext, MainActivity.class);
                Intent intent1 = new Intent();
                intent1.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(urlLink);
                intent1.setData(content_url);
                startActivities(new Intent[]{intentMain1, intent1});
                finish();
                break;
            case 2:
                Intent intentMain2 = new Intent(mContext, MainActivity.class);
                Intent intentde = new Intent(mContext, GoodsDetailActivity.class);
                intentde.putExtra("goodsId", tzId + "");
                startActivities(new Intent[]{intentMain2, intentde});
                finish();
                break;
            case 3:
                Intent intentMain3 = new Intent(mContext, MainActivity.class);
                Intent intent3 = new Intent(mContext, TravelDetailActivity.class);
                intent3.putExtra("tid", tzId);
                startActivities(new Intent[]{intentMain3, intent3});
                finish();
                break;
            case 4:
                Intent intentMain4 = new Intent(mContext, MainActivity.class);
                Intent intent4 = new Intent(mContext, TripDetailActivity.class);
                intent4.putExtra("tId", tzId);
                startActivities(new Intent[]{intentMain4, intent4});
                finish();
                break;
            case 5:
                Intent intentMain5 = new Intent(mContext, MainActivity.class);
                //  Intent intent5 = new Intent(mContext, FilmDetailActivity.class);
                Intent intent5 = new Intent(mContext, FilmCinemaListActivity.class);
                intent5.putExtra("fId", tzId);
                startActivities(new Intent[]{intentMain5, intent5});
                finish();
                break;
        }

    }

    @Override
    public void onBackPressed() {
        if (Jzvd.backPress()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        super.onPause();
        Jzvd.releaseAllVideos();
    }
}
